// KeySimulator.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "KeySimulator.h"


#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

extern "C" KEYSIMULATOR_API void SimulateKeyboard(WORD wKeyToPress, BOOL bPress, BOOL bRelease)
{
	KEYBDINPUT  kb={0};
	INPUT    Input={0};

	if (bPress)
	{
		kb.wVk = wKeyToPress;  
		Input.type = INPUT_KEYBOARD;

		Input.ki = kb;
		::SendInput(1,&Input,sizeof(Input));
	}

	if (bRelease)
	{
		// generate up 
		::ZeroMemory(&kb,sizeof(KEYBDINPUT));
		::ZeroMemory(&Input,sizeof(INPUT));
		kb.dwFlags  =  KEYEVENTF_KEYUP;
		
		kb.wVk = wKeyToPress;
		Input.type = INPUT_KEYBOARD;
		Input.ki = kb;
		::SendInput(1,&Input,sizeof(Input));
	}
}